#!/bin/bash
# 2012/09/25 Backup PGSQL
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH

Date="`date +%Y%m%d`"
Time="`date +%H%M`"
Backup_Dir="/home/backup"

###############################################################
cd /tmp
All_DB=(`echo "SELECT datname FROM pg_database;" | sudo -u postgres psql`)
cd - &> /dev/null
#echo ${#All_DB[@]}
if [ ! -d $Backup_Dir/$Date ]; then
      mkdir -p $Backup_Dir/$Date
fi
for ((i=3; i<$((${#All_DB[@]}-2)); i++))
   do
      pg_dump -U postgres  ${All_DB[$i]} | gzip > "$Backup_Dir/$Date/${All_DB[$i]}-$Time.gz"
done

#保留五天
find "$backupdir" -mtime +5 -exec rm -rf '{}' \;
